package com.hmall.search.web;


import com.hmall.feign.pojo.PageDTO;
import com.hmall.search.service.ISearchService;
import com.hmall.search.web.request.RequestParams;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private ISearchService searchService;
    @Autowired
    private RestHighLevelClient client;

    /**
     * 搜索栏自动补全功能
     * @param prefix
     * @return
     */
    @GetMapping("/suggestion")
    public List<String> getSuggestions(@RequestParam("key") String prefix){
        return searchService.getSuggestions(prefix);
    }

    /**
     * 基本搜索功能
     * @param params
     * @return
     */
    @PostMapping("/list")
    public PageDTO search(@RequestBody RequestParams params){
        return searchService.search(params);
    }


    // TODO 后期优化
    /**
     * 插入数据到索引库
     */
    @GetMapping("test")
    public void insertIndex() throws IOException {
        searchService.insert();
    }


    /**
     *  根据搜索关键字,获取到对应的品牌,分类
     * @return 对应的品牌,分类Map集合
     */
    @PostMapping("/filters")
    public Map<String, List<String>> getFilters(@RequestBody RequestParams params) throws IOException {

        return searchService.filters(params);
    }

}
